package com.design.composite;

import java.util.ArrayList;

/**
 * @author leali
 * @title: Folder
 * @projectName design
 * @description: 文件夹类：容器构件
 * @date 2022/1/2 22:54
 */
public class Folder extends AbstractFile{
    /**
     * 定义集合fileList，用于存储AbstractFile类型的成员
     */
    private final ArrayList<AbstractFile> fileList= new ArrayList<>();
    private final String name;

    public Folder(String name) {
        this.name = name;
    }
    @Override
    public void add(AbstractFile file) {
        fileList.add(file);
    }

    @Override
    public void remove(AbstractFile file) {
        fileList.remove(file);
    }

    @Override
    public AbstractFile getChild(int i) {
        return fileList.get(i);
    }

    @Override
    public void killVirus() {
        System.out.println("****对文件夹'" + name + "'进行杀毒");
        //递归调用成员构件的killVirus()方法
        for(AbstractFile obj : fileList) {
            obj.killVirus();
        }
    }
}
